//
//  VistorView.swift
//  sinaweibo
//
//  Created by IZEN on 16/3/8.
//  Copyright © 2016年 IZEN. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VistorViewDelegate : NSObjectProtocol{

    func userWillLogin()

    optional func userWillRegister()
}



class VistorView: UIView {
    
    weak var visitorDelegate:VistorViewDelegate?
    
    var didBtnClick : (() -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation(){
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        anima.repeatCount = MAXFLOAT
        anima.duration = 10
        anima.toValue = 2 * M_PI
        anima.removedOnCompletion = false
        circleIcon.layer.addAnimation(anima, forKey: nil)
    }
    
    //自定义视图
    private func setupUI(){
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(centerHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        
        }
        centerHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
            
            let value : CGFloat = 236.0/255.0
            backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
            
            
        }
        
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnlick", forControlEvents: .TouchUpInside)
    }
    

    
    //按钮的监听事件
    @objc private func loginBtnClick(){
//        didBtnClick?()
        visitorDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnlick(){
//        didBtnClick?()
        visitorDelegate?.userWillRegister?()
    }
    
    //MARK:外部调用的方法
    func setupUpInfoWith(title: String,imageName:String?){
        tipLabel.text = title
        
        if imageName != nil{
            circleIcon.image = UIImage(named: imageName!)
            centerHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }else{
            startAnimation()
        }
        
    }
    
    
    //懒加载子视图
    private lazy var maskImage :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var centerHouse : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLabel : UILabel = {
        let lb = UILabel()
        lb.textColor = UIColor.darkGrayColor()
        lb.font = UIFont.systemFontOfSize(14)
        lb.textAlignment = .Center
        lb.numberOfLines = 0
        lb.sizeToFit()
        return lb
        
    }()
    
    private lazy var loginBtn : UIButton = {
        let login = UIButton()
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        login.setTitle("登录", forState: .Normal)
        login.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(14)
        login.sizeToFit()
        return login
    }()
    
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(themColor, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        return btn
    }()
}
